package com.zapi.notification.controller;

import com.zapi.common.Result;
import com.zapi.notification.entity.Message;
import com.zapi.notification.service.NotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 通知Controller
 * 提供统一的通知接口
 */
@Api(tags = "通知服务")
@RestController
@RequestMapping("/notification/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 发送站内信
     */
    @ApiOperation("发送站内信")
    @PostMapping("/station-message")
    public Result<Boolean> sendStationMessage(@RequestBody Message message) {
        try {
            boolean success = notificationService.sendStationMessage(message);
            return success ? Result.success("站内信发送成功", true) : Result.error("站内信发送失败");
        } catch (Exception e) {
            return Result.error("站内信发送失败: " + e.getMessage());
        }
    }

    /**
     * 批量发送站内信
     */
    @ApiOperation("批量发送站内信")
    @PostMapping("/station-message/batch")
    public Result<Integer> sendBatchStationMessages(@RequestBody List<Message> messages) {
        try {
            int count = notificationService.sendBatchStationMessages(messages);
            return Result.success("成功发送 " + count + " 条站内信", count);
        } catch (Exception e) {
            return Result.error("批量发送站内信失败: " + e.getMessage());
        }
    }

    /**
     * 发送实时消息
     */
    @ApiOperation("发送实时消息")
    @PostMapping("/realtime-message")
    public Result<Boolean> sendRealTimeMessage(
            @ApiParam("用户ID") @RequestParam Long userId,
            @ApiParam("消息内容") @RequestParam String message) {
        
        try {
            boolean success = notificationService.sendRealTimeMessage(userId, message);
            return success ? Result.success("实时消息发送成功", true) : Result.error("实时消息发送失败");
        } catch (Exception e) {
            return Result.error("实时消息发送失败: " + e.getMessage());
        }
    }

    /**
     * 批量发送实时消息
     */
    @ApiOperation("批量发送实时消息")
    @PostMapping("/realtime-message/batch")
    public Result<Integer> sendBatchRealTimeMessages(
            @ApiParam("用户ID列表") @RequestBody List<Long> userIds,
            @ApiParam("消息内容") @RequestParam String message) {
        
        try {
            int count = notificationService.sendBatchRealTimeMessages(userIds, message);
            return Result.success("成功发送实时消息给 " + count + " 个用户", count);
        } catch (Exception e) {
            return Result.error("批量发送实时消息失败: " + e.getMessage());
        }
    }

    /**
     * 发送短信
     */
    @ApiOperation("发送短信")
    @PostMapping("/sms")
    public Result<Boolean> sendSms(
            @ApiParam("手机号") @RequestParam String phoneNumber,
            @ApiParam("模板编码") @RequestParam String templateCode,
            @ApiParam("模板参数") @RequestBody Map<String, Object> params) {
        
        try {
            boolean success = notificationService.sendSms(phoneNumber, templateCode, params);
            return success ? Result.success("短信发送成功", true) : Result.error("短信发送失败");
        } catch (Exception e) {
            return Result.error("短信发送失败: " + e.getMessage());
        }
    }

    /**
     * 批量发送短信
     */
    @ApiOperation("批量发送短信")
    @PostMapping("/sms/batch")
    public Result<Integer> sendBatchSms(
            @ApiParam("手机号列表") @RequestBody List<String> phoneNumbers,
            @ApiParam("模板编码") @RequestParam String templateCode,
            @ApiParam("模板参数") @RequestBody Map<String, Object> params) {
        
        try {
            int count = notificationService.sendBatchSms(phoneNumbers, templateCode, params);
            return Result.success("成功发送 " + count + " 条短信", count);
        } catch (Exception e) {
            return Result.error("批量发送短信失败: " + e.getMessage());
        }
    }

    /**
     * 发送邮件
     */
    @ApiOperation("发送邮件")
    @PostMapping("/email")
    public Result<Boolean> sendEmail(
            @ApiParam("收件人邮箱") @RequestParam String to,
            @ApiParam("邮件主题") @RequestParam String subject,
            @ApiParam("邮件内容") @RequestParam String content) {
        
        try {
            boolean success = notificationService.sendEmail(to, subject, content);
            return success ? Result.success("邮件发送成功", true) : Result.error("邮件发送失败");
        } catch (Exception e) {
            return Result.error("邮件发送失败: " + e.getMessage());
        }
    }

    /**
     * 批量发送邮件
     */
    @ApiOperation("批量发送邮件")
    @PostMapping("/email/batch")
    public Result<Integer> sendBatchEmails(
            @ApiParam("收件人邮箱列表") @RequestBody List<String> toList,
            @ApiParam("邮件主题") @RequestParam String subject,
            @ApiParam("邮件内容") @RequestParam String content) {
        
        try {
            int count = notificationService.sendBatchEmails(toList, subject, content);
            return Result.success("成功发送 " + count + " 封邮件", count);
        } catch (Exception e) {
            return Result.error("批量发送邮件失败: " + e.getMessage());
        }
    }

    /**
     * 发送模板邮件
     */
    @ApiOperation("发送模板邮件")
    @PostMapping("/email/template")
    public Result<Boolean> sendTemplateEmail(
            @ApiParam("收件人邮箱") @RequestParam String to,
            @ApiParam("模板编码") @RequestParam String templateCode,
            @ApiParam("模板参数") @RequestBody Map<String, Object> params) {
        
        try {
            boolean success = notificationService.sendTemplateEmail(to, templateCode, params);
            return success ? Result.success("模板邮件发送成功", true) : Result.error("模板邮件发送失败");
        } catch (Exception e) {
            return Result.error("模板邮件发送失败: " + e.getMessage());
        }
    }

    /**
     * 广播系统消息
     */
    @ApiOperation("广播系统消息")
    @PostMapping("/broadcast")
    public Result<Boolean> broadcastSystemMessage(@ApiParam("消息内容") @RequestParam String message) {
        try {
            boolean success = notificationService.broadcastSystemMessage(message);
            return success ? Result.success("系统消息广播成功", true) : Result.error("系统消息广播失败");
        } catch (Exception e) {
            return Result.error("系统消息广播失败: " + e.getMessage());
        }
    }

    /**
     * 发送消息到用户组
     */
    @ApiOperation("发送消息到用户组")
    @PostMapping("/group/{groupId}")
    public Result<Boolean> sendToUserGroup(
            @ApiParam("用户组ID") @PathVariable String groupId,
            @ApiParam("消息内容") @RequestParam String message) {
        
        try {
            boolean success = notificationService.sendToUserGroup(groupId, message);
            return success ? Result.success("用户组消息发送成功", true) : Result.error("用户组消息发送失败");
        } catch (Exception e) {
            return Result.error("用户组消息发送失败: " + e.getMessage());
        }
    }
} 